<template>
  <div class="container">
    <div class="app-container">
      <el-row type="flex" justify="star" style="padding: 10px">
        <el-col>
          <el-button type="primary" size="mini" @click="addPermission(0, 1)">添加权限</el-button>
        </el-col>
      </el-row>
      <el-table default-expand-all row-key="id" :data="permissionList" style="width: 100%" border>
        <el-table-column prop="name" label="名称" sortable width="300" align="center"> </el-table-column>
        <el-table-column prop="code" label="标识" sortable width="300" align="center"> </el-table-column>
        <el-table-column prop="description" label="描述" align="center"> </el-table-column>
        <el-table-column align="center">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addPermission(row.id, 2)"
              >添加</el-button
            >
            <el-button type="text" size="mini" class="el-icon-edit" @click="editPermission(row)"
              >编辑</el-button
            >
            <el-button type="text" size="mini" class="el-icon-delete" @click="delPermission(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="`${showText}权限点`" :visible.sync="showPerDialog" :before-close="btnCancel">
      <el-form ref="permissionFormRef" label-width="100px" :model="permissionForm" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" style="width: 500px"></el-input>
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model="permissionForm.code" style="width: 500px"></el-input
        ></el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="permissionForm.description" style="width: 500px"></el-input
        ></el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="permissionForm.enVisible"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            size="mini"
          ></el-switch>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOk">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPermissionList,
  addPermission,
  updatePermission,
  getPermissionDetail,
  delPermission
} from '@/api/permission.js'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showPerDialog: false,
      permissionForm: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: null, // 权限类型 当type为1时，为一级权限，当type为2时，为二级权限 二级权限应该添加在一级权限下，二者是通过id和pid进行关联的
        pid: null, // 父级id
        enVisible: 0 // 是否启用
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = tranListToTreeData(await getPermissionList(), 0)
      this.permissionList = res
    },
    // 添加权限
    addPermission(id, type) {
      this.permissionForm.pid = id
      this.permissionForm.type = type
      this.showPerDialog = true
    },
    // 编辑权限
    async editPermission(row) {
      const res = await getPermissionDetail(row.id)
      this.permissionForm = res
      this.showPerDialog = true
    },
    btnOk() {
      this.$refs.permissionFormRef.validate(async valid => {
        if (valid) {
          if (this.permissionForm.id) {
            //编辑模式
            await updatePermission(this.permissionForm)
            this.$message.success('更新权限成功!')
          } else {
            //新增模式
            await addPermission(this.permissionForm)
            this.$message.success('新增权限成功!')
          }
          this.getPermissionList()
          this.btnCancel()
        }
      })
    },
    // 关闭弹窗
    btnCancel() {
      this.permissionForm = {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: null, // 权限类型
        pid: null, // 父级id
        enVisible: 0 // 是否启用
      }
      this.showPerDialog = false
      this.$refs.permissionFormRef.resetFields()
    },
    async delPermission(id) {
      try {
        await this.$confirm('确定删除该权限?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delPermission(id)
        this.$message.success('删除成功!')
        this.getPermissionList()
      } catch (error) {
        this.$message.error('删除权限!')
      }
    }
  },
  computed: {
    showText(){
      return this.permissionForm.id ? '编辑' : '添加'
    }
  }
}
</script>

<style></style>
